<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'qtrk:page'"
        :addPer="'qtrk:save'"
        addTitle="新增"
        @search="search"
        @insert="add"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.status"
          placeholder="库存状态"
        >
          <el-option
            v-for="item in getDict(['库存状态'])['库存状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <CarModel style="width: 350px" v-model="modelList" ref="carModel" />
        <el-date-picker
          v-model="form.inTime"
          type="datetimerange"
          start-placeholder="创建日期"
          end-placeholder="创建日期"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-input
          v-model="form.client"
          clearable
          placeholder="请输入入库号"
          style="width: 200px"
        />
        <el-input
          v-model="form.vin"
          clearable
          placeholder="请输入vin码"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
        @row-dblclick="handleDetails"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="修改"
            v-if="row.status!=='已入库'"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'qtrk:save'"
            @click="updateUser(row)"
          ></i>

          <i
          v-if="row.status!=='已入库'"
            class="el-icon-s-check"
            title="入库"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'qtrk:inStock'"
            @click="inStock(row)"
          ></i>
        </template>
      </data-table>
      <detailsDialog
        :dialog-visible="dialogVisible"
        @set-visible="setVisible"
        :ruleFormDialog="ruleFormDialog"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import { CarModel } from '@/components/index'
import detailsDialog from './components/detailsDialog'
import {
  dataList,
  details,
  inStock,
} from '@/api/pre-sale/newCarStock/putInStorageManagement/otherInventory'
import { getDict } from '@/utils/index'
// import { details } from '@/api/after-sale/finance/assets'

export default {
  components: {
    CarModel,
    detailsDialog,
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dataList,
     // findDataById: details,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.brandSeriesModel =
            data.carBrandName + '/' + data.seriesName + '/' + data.modelName
          return data
        })
      },
    }),
  ],
  data() {
    return {
      ruleFormDialog: {},
      dialogVisible: false,
      tableDataTwo: [],
      modelList: [],
      searchOptions: { allotType: true },
      getDict: getDict,
      form: {
        vin:'',
        brandId: '',
        seriesId: '',
        modelId: '',
      },
      tableColumnAttributes: [
        {
          name: '门店',
          value: 'storeName',
          type: 'text',
        },
        // {
        //   name: '入库来源单号',
        //   value: 'sourceCode',
        //   type: 'text',
        // },
        {
          name: '其他入库单号',
          value: 'code',
          type: 'text',
        },
        {
          name: '仓库',
          value: 'storageName',
          type: 'text',
        },
        // {
        //   name: '仓库位置',
        //   value: 'address',
        //   type: 'text',
        // },

        {
          name: '仓位',
          value: 'storageSiteName',
          type: 'text',
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text',
        },
        {
          name: '品牌/车系/车型',
          value: 'brandSeriesModel',
          type: 'text',
        },
        {
          name: '状态',
          value: 'status',
          type: 'text',
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text',
        },
        {
          name: '创建人',
          value: 'createUserName',
          type: 'text',
        },
        {
          name: '入库缘由',
          value: 'rkDesc',
          type: 'text',
        },
      ],
      dict: {},
    }
  },
  methods: {
    setVisible(val) {
      this.dialogVisible = val
    },
   async handleDetails(row) {
      this.dialogVisible = true

  const {data}=await details(row.id)

 this.ruleFormDialog = {
        ...data,
      }
    },
    inStock(row) {
      this.$confirm('是否确定入库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await inStock(row.id)
          this.setTableData()
        })
        .catch(() => {})
    },
    add() {
      this.$router.push({
        name: 'addOtherInventoryInfo',
      })
    },
    // 修改
    updateUser(row) {
      const { id } = row
      this.$router.push({
        name: 'editOtherInventoryInfo',
        params: { id },
      })
    },
    // 查询
    search(form) {
      if (this.modelList.length > 0) {
        form.brandId = this.modelList[0]
        form.seriesId = this.modelList[1]
        form.modelId = this.modelList[3]
      } else {
        form.brandId = ''
        form.seriesId = ''
        form.modelId = ''
      }
      if (form.inTime && form.inTime.length > 0) {
        form.inBoundMinTime = form.inTime[0]
        form.inBoundMaxTime = form.inTime[1]
      } else {
        form.inBoundMinTime = ''
        form.inBoundMaxTime = ''
      }
      delete form.inTime
      this.searchOptions = { ...form, allotType: true }
      this.currentPage = 1
      this.setTableData()
    },
  },
}
</script>
